<?php
!defined('P_W') && exit('Forbidden');

class PW_House_BrokerGroupDB extends BaseDB {
	var $_tableName = "pw_house_brokergroup";
	var $_primaryKey = 'groupid';
	/**
	 * 获取所有经纪人组信息
	 */
	function findAll() {
		$_sql = 'SELECT * FROM '.$this->_tableName;
		$query = $this->_db->query($_sql);
		return $this->_getAllResultFromQuery($query);
	}
	
	/**
	 * 向经纪人组添加用户
	 * @param $groupid
	 * @param $num
	 */
	function addUsernum($groupid,$num=1) {
		$num = (int) $num;
		$_sql = 'UPDATE '.$this->_tableName.' SET usernum=usernum+'.$num.' WHERE groupid='.pwEscape($groupid);
		$this->_db->update($_sql);
	}
	/**
	 * 经纪人组删除用户
	 * @param $groupid
	 * @param $num
	 */
	function reduceUsernum($groupid,$num=1) {
		$num = (int) $num;
		$temp = $this->get($groupid);
		if ($temp['usernum']<$num) return false;
		$_sql = 'UPDATE '.$this->_tableName.' SET usernum=usernum-'.$num.' WHERE groupid='.pwEscape($groupid);
		$this->_db->update($_sql);
	}
	/**
	 * 获取单条数据
	 */
	function get($groupid) {
		return $this->_get($groupid);
	}
	/**
	 * 更新某条数据
	 * @param $fieldData
	 * @param $groupid
	 */
	function update($fieldData,$groupid) {
		return $this->_update($fieldData,$groupid);
	}
	/**
	 * 删除一条数据
	 * @param $groupid
	 */
	function delete($groupid) {
		return $this->_delete($groupid);
	}
	/**
	 * 添加一条数据
	 * @param $fieldData
	 */
	function insert($fieldData) {
		return $this->_insert($fieldData);
	}
	
	function getStruct() {
		return array('groupid', 'name','usernum');
	}
}